<template>
  <div class="theDocumentary">
    <div>
<!--      jmc-->
      <div class="cgName"><b>采购计划</b> </div>
      <div style="margin-top: 15px">
        <el-radio-group v-model="queryParams.selectType" size="medium" @change="jihuaChange(queryParams.selectType)">
          <el-radio-button label="0" >计划列表</el-radio-button>
          <el-radio-button label="1">今日新增</el-radio-button>
          <el-radio-button label="2">本周新增</el-radio-button>
          <el-radio-button label="3">本月新增</el-radio-button>
        </el-radio-group>
        <el-button type="success" size="medium"  @click="newAdd" >新增</el-button>
        <div class="pageTop" style="margin-top: 15px">
          <el-form :model="queryParams" ref="queryParams" :inline="true" class="inputWidth"  style="padding: 0px 10px; padding-top: 20px;" >
            <el-form-item label="采购计划单号" >
              <el-input
                v-model="queryParams.purchasePlanNo"
                placeholder="请输入采购计划单号"
                clearable
                size="medium"
              />
            </el-form-item>
            <el-form-item label="状态" >
              <el-select
                v-model="queryParams.purchasePlanStatus"
                placeholder="请选择"
                clearable
                size="medium">
                <el-option
                  v-for=" item in ztList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="仓库" >
              <el-select
                v-model="queryParams.warehouseId"
                placeholder="请选择"
                clearable
                size="medium">
                <el-option
                  v-for="item in camgkuList"
                  :key="item.warehouseId"
                  :label="item.warehouseName"
                  :value="item.warehouseId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="采购日期">
              <el-date-picker
                v-model="djDateList"
                @change="riqiChange(djDateList)"
                size="medium"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" size="medium" @click="getList">搜索</el-button>
            </el-form-item>
          </el-form>

        </div>
        <el-table v-loading="loading" :data="dataList" stripe border style="margin-top: 20px" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
          <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" label="采购计划单号"   property="purchasePlanNo"  width="200" ></el-table-column>
          <el-table-column align="center" label="单据名称"   property="purchasePlanName"  width="200" ></el-table-column>
          <el-table-column align="center" label="仓库名称"   property="warehouseName"  width="200" ></el-table-column>
          <el-table-column align="center" label="采购计划日期"   property="purchasePlanDate"  width="180"  ></el-table-column>
          <el-table-column align="center" label="状态"   property="purchasePlanStatus" >
            <template slot-scope="scope">
              <span v-show="scope.row.purchasePlanStatus=='2'">待审核</span>
              <span v-show="scope.row.purchasePlanStatus=='3'">审核通过</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="计划人员"   property="createUserName"   ></el-table-column>
          <el-table-column align="center" label="审核人员"   property="checkUserName"   ></el-table-column>
          <el-table-column align="center" label="审核时间"   property="checkDate"  width="180" ></el-table-column>
          <el-table-column align="center" label="采购计划数量"   property="purchasePlanAllqty"  width="180"  ></el-table-column>
          <el-table-column align="center" label="采购计划金额"   property="purchasePlanAllmoney"   width="180" ></el-table-column>
          <el-table-column align="center" label="审核日期"   property="checkDate"   width="180" ></el-table-column>
          <el-table-column align="center" label="备注"   property="remark"   width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="管理" width="250" fixed="right">
            <template slot-scope="scope">
<!--              <el-button v-show="scope.row.purchasePlanStatus=='1' ||scope.row.purchasePlanStatus=='4' " size="mini" type="success"   @click="tijiao( scope.row)">提交</el-button>-->
              <el-button  size="mini" type="primary"   @click="edit( scope.row)">查看</el-button>
              <el-button v-if="deleteQX==1" v-show="scope.row.purchasePlanStatus=='2' ||scope.row.purchasePlanStatus=='4' " size="mini" type="danger"   @click="handleDelete( scope.row)">删除</el-button>
              <el-button v-if="auditQX==1" v-show="scope.row.purchasePlanStatus=='2' ||scope.row.purchasePlanStatus=='4'" size="mini" type="warning"   @click="shBtn( scope.row)">审核</el-button>
              <el-button v-if="auditQX==1" v-show="scope.row.purchasePlanStatus=='3'" size="mini" type="warning"   @click="fsBtn( scope.row)">反审</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-top: 20px;text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :current-page="queryParams.current"
          :page-size="queryParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </div>
    <add-Dialog v-if="newShow" :addData="addData" @callback="callback"></add-Dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import {getnewDat,getCurrWeek,getCurrentMonthFirst} from "@/utils/index"

import addDialog from './addDialog'
export default {
  components:{
    addDialog

  },
  data() {
    return {
      camgkuList:[],
      newShow:false,
      addData:{},
      total:0,
      loading:false,
      radio:'1',
      queryParams:{
        current:1,
        size:10,
        purchasePlanStatus:'',
        purchasePlanNo:'',
        purchasePlanTimeEnd:'',
        purchasePlanTimeStart:'',
        selectType:'0',

      },
      //供应商
      gysList:[
        {value:'1',label:'供应商1'},
        {value:'2',label:'供应商2'},
        {value:'3',label:'供应商3'},
      ],
      //状态
      ztList:[

        {value:'2',label:'待审核'},
        {value:'3',label:'审核通过'},

      ],
      //单据日期
      djDateList:[],
      //列表数据
      dataList:[],

      deleteQX:localStorage.getItem("delete"),//删除权限
      addQX:localStorage.getItem("add"),//保存权限
      auditQX:localStorage.getItem("audit"),//审核权限

    }
  },
  created() {
    console.log('权限 删除  保存  审核',this.deleteQX,this.addQX,this.auditQX)

    //获取仓库
    this.getcangku()
    this.getList()

  },
  methods: {

    //获取仓库
    getcangku:function (){
      request({
        url: '/wh/warehouse/getFinishedProducts',
        method: 'get',
      }).then(response => {
        this.camgkuList = response.data
      });
    },
    //计划列表
    jihuaChange:function (row){

      if(row==0){
        this.djDateList=[]
        this.queryParams.purchasePlanTimeStart =''
        this.queryParams.purchasePlanTimeEnd = ''
        this.getList()
      }else if(row==1){
        this.djDateList=getnewDat()
        this.queryParams.purchasePlanTimeStart = this.djDateList[0]
        this.queryParams.purchasePlanTimeEnd = this.djDateList[1]
        this.getList()
      }else if(row==2){
        this.djDateList=getCurrWeek()
        this.queryParams.purchasePlanTimeStart = this.djDateList[0]
        this.queryParams.purchasePlanTimeEnd = this.djDateList[1]
        this.getList()
      }else if(row==3){
        this.djDateList=getCurrentMonthFirst()
        this.queryParams.purchasePlanTimeStart = this.djDateList[0]
        this.queryParams.purchasePlanTimeEnd = this.djDateList[1]
        this.getList()
      }
      console.log('计划监听时间',this.djDateList)

    },

    //单据日期查询
    riqiChange:function (row){
      console.log('监听日期',row)
      if(row){
        this.queryParams.purchasePlanTimeStart = row[0]
        this.queryParams.purchasePlanTimeEnd = row[1]
      }else {
        this.queryParams.purchasePlanTimeStart = ''
        this.queryParams.purchasePlanTimeEnd = ''
      }

    },
    //新增
    newAdd:function (){
      var obj={}
      obj.leixing='post'
      this.addData = obj
      this.newShow = true

    },
    //修改
    edit:function (row){
      var obj={}
      obj.leixing='put'
      obj.id=row.purchasePlanId
      obj.purchasePlanStatus=row.purchasePlanStatus
      this.addData = obj
      this.newShow = true
    },
    //分页查询
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.current = val
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.size = val
      this.getList()
    },
    //弹框返回事件
    callback:function (item){
      this.newShow = false
      this.getList()
    },
    //搜索
    getList:function (){
      this.loading=true
      request({
        url: '/wh/purchasePlan/page',
        method: 'get',
        params:this.queryParams
      }).then(response => {
        this.loading=false
        if(response.code=='200'){
          this.dataList = response.data.records
          this.total =response.data.total
        }else {
          this.dataList=[]
          this.$message({
            type: 'warning',
            message:response.msg
          });
        }
      });
    },
    //提交
    tijiao:function (row){
      this.$confirm('此操作将提交该单号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tijiaoBtn(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消提交'
        });
      });
    },
    tijiaoBtn:function (row){
      request({
        url: '/wh/purchasePlan/submit?id='+row.purchasePlanId,
        method: 'put',
        data:row.purchasePlanId
      }).then(response => {
        this.$message({
          type: 'success',
          message:response.msg
        });
        this.getList()
      });
    },
    //删除
    handleDelete:function (row){
      this.$confirm('此操作将永久删除该单号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
       this.shanchu(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //删除接口
    shanchu:function (row){
      request({
        url: '/wh/purchasePlan/del?ids='+row.purchasePlanId,
        method: 'post',
      }).then(response => {
        this.$message({
          type: 'success',
          message:response.msg
        });
        this.getList()
      });
    },
    //审核/wh/purchasePla/audit
    shBtn:function (row){
      this.$confirm('此操作将审核通过, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.ShBtn(row);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消审核'
        });
      });
    },
    //审核
    ShBtn:function (row){
      request({
        url: '/wh/purchasePlan/audit?id='+row.purchasePlanId,
        method: 'put',
      }).then(response => {
        if(response.code==200){
          this.$message({
            type: 'success',
            message:response.msg
          });
        }else {
          this.$message({
            type: 'warning',
            message:response.msg
          });
        }
        this.getList()
      });
    },
    //驳回
    fsBtn:function (row){
      this.$confirm('此操作将反审, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.bohui(row);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消反审'
        });
      });
    },
    //驳回
    bohui:function (row){
      request({
        url: '/wh/purchasePlan/rejected?id='+row.purchasePlanId,
        method: 'put',
      }).then(response => {
        if(response.code==200){
          this.$message({
            type: 'success',
            message:response.msg
          });
        }else {
          this.$message({
            type: 'warning',
            message:response.msg
          });
        }
        this.getList()
      });
    },

  }
}
</script>

<style  scoped >
.theDocumentary {

}
.theDocumentary .cgName {
  font-size: 20px;
}

</style>
